$(function(){
    initSubmitEvent();
});
function initSubmitEvent(){
    $('#submitForm').validate({
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(form){
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
                jQuery.data(this, "disabledOnSubmit", {submited : true});
                //异步提交
                var requestJson = $(form).serializeJSON();
                var that = this;
                if(confirm("确认上架该商品嘛？")) {
                    $.ajax({
                        url: form.action,
                        type: "POST",
                        contentType: 'application/json;charset=utf-8', //设置请求头信息
                        dataType: "json",
                        data: JSON.stringify(requestJson),
                        success: function (data) {
                            if (data.code == 0) {
                                alert(data.msg);
                                window.location.href = appServer + "/wholesaleArea/list.htm";
                            } else {
                                alert(data.msg);
                                jQuery.removeData(that, "disabledOnSubmit");
                            }
                        },
                        error: function (res) {
                            alert(res.responseText);
                            jQuery.removeData(that, "disabledOnSubmit");
                        }
                    });
                }else{
                    jQuery.removeData(that, "disabledOnSubmit");
                }
            } else {
                // repeat submit ignore;
            }
        },
        success:function(element){
        },
        errorPlacement : function(error, element) {
            if(error.text()){
                if(element.next("span[class='red']").size() == 0){
                    element.after("<span class='red'>" + error.text() +"</span>");
                }else{
                    element.next("span[class='red']").text(error.text());
                }
            }else{
                element.next("span[class='red']").remove();
            }
        },
        rules:{
            "goodsId":{required:true},
	        "payQuotaId":{required:true},
            "wholesaleLimit":{required:true,isIntGteZero:true}
        },
        messages:{
        }
    });
}



var optionSelectValue = new Array(20);//用于已选择的规格属性
var index = 0;
function addrow(goodsId){
    jQuery.ajax({
        type:"post",
        url:appServer + "/goods/ajax/getAttribute.htm",
        data:{"goodsId":goodsId},
        dataType:"json",
        success:function(message){
            var attributeInfoList = message.attributeInfoList;
            var checkAtttribute = message.atributeNameInfo;//该商品已经存在的规格,只有规格编辑时才有值
            var htmlValue=new Array(attributeInfoList.length);
            var htmlAttributeName=new Array(attributeInfoList.length);
            var checkedArray = new Array(checkAtttribute==null?20:checkAtttribute.length);
            if (checkAtttribute != null){
                //将该商品已存在的属性放入检查数组中
                for (var i = 0;i<checkAtttribute.length;i++){
                    var attribute = checkAtttribute[i];
                    checkedArray[i]= attribute.attributeFormList[0].attributeValue;
                }
            }
            for(var i=0;i<attributeInfoList.length;i++ ) {
                var attributeInfo = attributeInfoList[i];
                if (optionSelectValue.indexOf($("#"+attributeInfo.attributeName+"  option:selected").text()) > -1) {
                    //如果当前属性已经选择过，则不再新增行
                    alert("您已选择该套餐，不能重复选择");
                    return;
                }
                if (checkedArray.indexOf($("#"+attributeInfo.attributeName+"  option:selected").text()) > -1){
                    //如果该商品已存在该属性，则不再新增行
                    alert("您已选择该套餐，不能重复选择");
                    return;
                }
                htmlValue[i] = $("#"+attributeInfo.attributeName+"  option:selected").text();
                htmlAttributeName[i] = '<input type="hidden" name="attributeInfoList[][attributeFormList][][attributeName]" value="'+attributeInfo.attributeName+'" />'
            }
            var c=document.getElementById('mytable');//获得表格的信息
            if( c.rows.length==0){//如果是向一个空表增加一行
                var x=c.insertRow(0);//向空表插入一行
                var y=x.insertCell(0);//向新行插入一列
                y.innerHTML="new cell0";
            }
            else{
                var z=c.rows[0].cells;//如果不是空表，首先获得表格有多少列，先获取再插入新行
                var x=c.insertRow(1);

                var y0=x.insertCell(0);
                y0.style="display:none;"
                y0.innerHTML = "<input name='attributeInfoList[][skuId]' type='hidden' value=''/>";
                var y1=x.insertCell(1);
                y1.innerHTML="<input name='attributeInfoList[][stockNum]' type='text' maxlength='8' onkeyup='onlyNum(this);'/>";
                var y2=x.insertCell(2);
                y2.innerHTML="<input name='attributeInfoList[][itemPrice]' type='text' onkeyup='checkvalue(this)' maxlength='10'/>";
                var y3=x.insertCell(3);
                y3.innerHTML="<input name='attributeInfoList[][quotaAmount]' type='text' maxlength='8' onkeyup='onlyNum(this);'/>";

                for(var i=4;i<z.length;i++){//依次向新行插入表格列数的单元格
                    var y=x.insertCell(i);
                    y.innerHTML="<input name='attributeInfoList[][attributeFormList][][attributeValue]' type='text' style='border:none;background-color: rgb(242,245,249);' readonly value='"+htmlValue[i-4]+"'/>"+htmlAttributeName[i-4];
                }
                optionSelectValue[index++]=htmlValue[0];
            }
        },
        error:function(xhr, message, e){

        }
    });
}
function delrow(goodsId){
    jQuery.ajax({
        type:"post",
        url:appServer + "/goods/ajax/getAttribute.htm",
        data:{"goodsId":goodsId},
        dataType:"json",
        success:function(message){
            var checkAtttribute = message.atributeNameInfo;//该商品已经存在的规格,只有规格编辑时才有值
            var x=document.getElementById("mytable");
            if (checkAtttribute != null && checkAtttribute.length==(x.rows.length-1)){
                alert("已存在的套餐不允许删除");
                return;
            }
            if (x.rows.length-1 > 1){
                x.deleteRow(1);//删除一行
                optionSelectValue.splice(--index,1);
            }
        },
        error:function(xhr, message, e){

        }
    });
}